<template>
  <div>
    <el-dialog
      :title="diaInfo.isAdd ? '添加商品规格' : '编辑商品规格'"
      :visible.sync="diaInfo.isShow"
      :before-close="cancel"
      center
    >
      <el-form :model="specsForm" ref="specsRef" :rules="rules">
        <el-form-item label="规格名称" :label-width="formLabelWidth" prop="specsname">
          <el-input
            v-model="specsForm.specsname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          v-for="(item, index) in attrArr"
          :label-width="formLabelWidth"
        >
          <el-input autocomplete="off" class="left" v-model="item.value"></el-input>
          <el-button type="primary" @click="addArr" v-if="index == 0"
            >新增规格属性</el-button
          >
          <el-button v-else @click="delArr(index)">删除</el-button>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-tooltip :content="'Switch value: ' + value" placement="top">
            <el-switch
              v-model="specsForm.status"
              active-color="#13ce66"
              inactive-color="#ccc"
              :active-value="1"
              :inactive-value="2"
            >
            </el-switch>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { specsAdd, specsEdit, specsInfo } from "../../request/api.js";
import { mapActions } from "vuex";
export default {
  props: ["diaInfo"],
  data() {
    return {
      specsForm: {
        specsname: "", //商品规格名称
        attrs: "", //商品规格属性值
        status: 1, // 状态1正常2禁用
      },
      rules: {
        specsname: [
          { required: true, message: "请输入规格名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
      },
      formLabelWidth: "80px",
      value: "1",
      attrArr: [
        {
          value: "",
        },
      ],
    };
  },
  methods: {
    // 辅助函数
    ...mapActions({
      getSpecsListActions: "specs/getSpecsListActions",
      getCountActions: "specs/getCountActions",
      changePageActions: "specs/changePageActions",
    }),
    // 表单添加事件
    addArr(index) {
      // console.log(1);
      if (this.attrArr.length > 6) {
        this.$message.warning("不能再多了哦");
        return;
      }
      this.attrArr.push({
        value: "",
      });
    },
    // 删除
    delArr(index) {
      this.attrArr.splice(index, 1);
    },
    // 封装取消
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    // 封装清空数据事件
    reset() {
      this.specsForm = {
        specsname: "", //商品规格名称
        attrs: "", //商品规格属性值
        status: 1, // 状态1正常2禁用
      };
      this.attrArr = [
        {
          value: "",
        },
      ];
      // 清空规则验证
      this.$refs.specsRef.resetFields();
    },
    // 封装一个数据回显
    lookup(id) {
      specsInfo({ id }).then((res) => {
        // console.log(res);
        this.specsForm = res.list[0];
        this.specsForm.id = id;

        this.specsForm.attrs.map((item, index) => {
          // console.log(item);
          if (index == 0) {
            this.attrArr[0].value = item;
          } else {
            this.attrArr.push({
              value: item,
            });
          }
        });
      });
    },
    // 商品添加
    confirm() {
      // console.log(this.specsForm);
      this.$refs.specsRef.validate((val) => {
        if (val) {
          this.specsForm.attrs = this.attrArr.map((item) => item.value).join(",");
          if (this.diaInfo.isAdd) {
            specsAdd(this.specsForm).then((res) => {
              // console.log(res,'添加成功了');
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                // 重新调用
                this.getSpecsListActions();
                this.getCountActions();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            specsEdit(this.specsForm).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getSpecsListActions();
                this.getCountActions();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
  },
  mounted() {},
  watch: {},
  computed: {},
  filters: {},
  components: {},
};
</script>
<style scoped>
.left {
  width: 75%;
}
</style>
